const mysql = require("mysql2");
const appConfig = require("../config/config");
const global_data = {};

function CreateMysqlConn() {
  let sqlConfig = {
    connectionLimit: 10,
    host: appConfig.mysql.host,
    user: appConfig.mysql.user,
    password: appConfig.mysql.password,
    database: appConfig.mysql.dbname,
    port: appConfig.mysql.port,
    timezone: "+00:08",
    multipleStatements: true, //支持执行多条sql语句
  };
  //
  let connPool = mysql.createPool(sqlConfig);
  // connection.connect(function(err) {
  //   if (err) {
  //     console.error('数据库连接失败，err: ' + err.stack);
  //     return;
  //   }
  //   //
  //   // console.log('数据库连接成功，连接id：' + connection.threadId);
  // });

  global_data.connPool = connPool;
  return connPool;
}

function CloseMysqlConn() {
  global_data.connPool.end();
}

module.exports = {
  CreateMysqlConn,
  CloseMysqlConn,
  global_data,
};
